package edu.jmu.service.impl;

import edu.jmu.dao.NewsDao;
import edu.jmu.domain.News;
import edu.jmu.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;



    @Override
    public List<News> findAll() {
        return newsDao.findAll();
    }

    @Override
    public List<News> findByKeyword(String str) {
        return newsDao.findByKeyword(str);
    }

    @Override
    public List<News> findByKeywordDrafts(String str) {
        return newsDao.findByKeywordDrafts(str);
    }

    @Transactional
    @Override
    public int addNews() {
        News news = new News();
        news.setBody("");
        news.setTitle("");
        news.setWriter("");
        news.setSummaryInfo("");
        news.setReleaseStatus("否");
        news.setReleaseDate(new Date());
        newsDao.insertNews(news);
        return news.getId();
    }

    @Transactional
    @Override
    public void updateNews(News news) {
        newsDao.updateNews(news);
    }

    @Transactional
    @Override
    public void releaseNews(News news) {
        newsDao.updateNews(news);
    }

    @Transactional
    @Override
    public void deleteNews(List<Integer> list) {
        for (int i:list) {
            newsDao.deleteNews(i);
        }
    }

    @Override
    public News findById(int id) {
        return newsDao.findById(id);
    }

    @Override
    public List<News> findDraftsInfo() {
        return newsDao.findDraftsInfo("<p><br></p>");
    }


}
